import { maccApi, requestHttp } from '@utils/index';
import { i18nRender } from '@/locales';
let timer1, timer2;
const state = {
  nbr_intf: null, //多三层口情况，选中的三层口
  netTopoType: null, //存的是网络的拓扑类型，而不是某个三层口分支的
  topoType: null, //有可能拓扑是多三层口的，topoType 存储的是当前选中三层口分支的拓扑类型
  topoNetworkMode: null,
  commonTopoData: null, //拓扑数据
  vlanData: [], //网络下的所有vlan
  selectVlanId: null, //当前选中显示的vlanId
  noSupportInfo: {}, //不支持的code和原因
  selectVlanInfo: null, //当前选中的vlan 的信息
  staticParams: null, //静态IP 回指路由信息
  dhcpList: [], //可配置的dhcp列表
  netSsidList: [],
  virtualDevInfo: null, //虚拟设备补齐为网管交换机的设备信息
  biz_id: null, //用于埋点（一次业务网配置的标识id）
  startTime: null, //诸葛度量：开始业务配置的时间戳（点击有线/无线 添加或者配置，出现抽屉弹窗开始）
  isVlanDhcp: false, //是否跳转过来默认设置dhcp
};

const mutations = {
  SET_NET_TOPO_TYPE(state, payload) {
    //整网的拓扑类型
    if (self != top) {
      if (top.$('#businessmap_container').length > 0) {
        top.$('#businessmap_container').data('netWorktopoType', payload);
      } else {
        timer1 && clearInterval(timer1);
        timer1 = setInterval(() => {
          if (top.$('#businessmap_container').length > 0) {
            top.$('#businessmap_container').data('netWorktopoType', payload);
            timer1 && clearInterval(timer1);
          }
        }, 500);
      }
    }
    state.netTopoType = payload;
  },

  SET_TOPO_TYPE(state, payload) {
    //有可能拓扑是多三层口的，topoType 存储的是当前选中三层口分支的拓扑类型
    state.topoType = payload;
  },

  SET_START_CONF_TINE(state, payload) {
    state.startTime = payload;
  },

  SET_SELECT_NBR_INTF(state, payload) {
    state.nbr_intf = payload;
  },

  SET_VLAN_DATA(state, payload) {
    state.vlanData = payload;
  },

  SET_NET_SSID_LIST(state, payload) {
    state.netSsidList = payload;
  },

  SET_SELECT_VLAN_Id(state, payload) {
    //通知父页面更新
    if (self != top) {
      top.PubSub &&
        top.PubSub.publish('UPDATE_BUSINESS_PAGE', {
          noNeedUpdateVue: true,
          selectVlan: payload,
        }); // UPDATE_BUSINESS_PAGE 中会触发新框架更新，noNeedUpdateVue表示不触发新框架更新
    }
    state.selectVlanId = payload;
  },
  //从AI排障过来需要设置地址池
  SET_VLAN_DHCP(state, payload) {
    state.isVlanDhcp = payload;
  },

  SET_NO_SUPPORT_INFO(state, payload) {
    state.noSupportInfo = payload;
  },

  SET_VIRTUAL_MANAGE_SW_INFO(state, payload) {
    state.virtualDevInfo = payload;
  },

  CLEAR_BUSINESS_STATE(state) {
    state.nbr_intf = null; //多三层口情况，选中的三层口
    state.topoType = null; //拓扑类型
    state.commonTopoData = null; //拓扑数据
    state.vlanData = []; //网络下的所有vlan
    state.selectVlanId = null; //当前选中显示的vlanId
    state.noSupportInfo = {}; //不支持的code和原因
    state.selectVlanInfo = null; //当前选中的vlan 的信息
    state.staticParams = null; //静态IP 回指路由信息
    state.dhcpList = []; //可配置的dhcp列表
    state.netSsidList = []; //获取网络下的ssid列表
    state.virtualDevInfo = null; //虚拟设备补齐为网管交换机的设备信息
    state.biz_id = null;
    state.startTime = null;
  },

  SET_TOPO_DATA(state, payload) {
    state.commonTopoData = payload;
  },

  SET_SELECT_VLAN_INFO(state, payload) {
    state.selectVlanInfo = payload;
  },

  SET_STATIC_CONF_PARAMS(state, payload) {
    state.staticParams = payload;
  },

  SET_DHCP_LIST(state, payload) {
    state.dhcpList = payload;
  },

  SET_BIZ_ID(state, payload) {
    console.log('创建Biz_id', payload);
    state.biz_id = payload;
    if (self != top) {
      top.bizMapMask = {
        biz_id: payload,
      };
    }
  },
};

const actions = {
  //获取拓扑类型和配置支持情况
  async getTopoTypeAndSupport({ commit }, params) {
    const { groupId } = params;
    const { nbr_intf } = state;
    let api = `${maccApi.GET_BUSINESS_CHECK}?group_id=${groupId}&version=MACC`;
    if (nbr_intf) {
      api += `&nbr_intf=${nbr_intf}`;
    }
    const {
      code,
      msg = i18nRender('businessNet.tips8'),
      topoType,
      // topoNetworkMode,
      checkCode,
      checkMsg,
    } = await requestHttp({
      api,
      method: 'GET',
    });
    const noSupportInfo = { checkCode, checkMsg };
    if (code == 0) {
      //通知父页面更新
      console.log('state.topoType', state.topoType);

      if (self != top && state.topoType != topoType) {
        top.PubSub &&
          top.PubSub.publish('UPDATE_BUSINESS_PAGE', { noNeedUpdateVue: true }); // UPDATE_BUSINESS_PAGE 中会触发新框架更新，noNeedUpdateVue表示不触发新框架更新
      }
      commit('SET_TOPO_TYPE', topoType);
      commit('SET_NO_SUPPORT_INFO', noSupportInfo);
      if (self != top) {
        if (top.$('#businessmap_container').length > 0) {
          top.$('#businessmap_container').data('topoType', topoType);
        } else {
          timer2 && clearInterval(timer2);
          timer2 = setInterval(() => {
            if (top.$('#businessmap_container').length > 0) {
              top.$('#businessmap_container').data('topoType', topoType);
              timer2 && clearInterval(timer2);
            }
          }, 500);
        }
      }
    }
    return { code, msg };
  },

  //获取vlan列表
  async getBusinessVlanList({ commit }, params) {
    const { groupId } = params;
    const { nbr_intf } = state;
    let url = `${maccApi.GET_BUSINESS_NET_VLAN}?group_id=${groupId}`;
    if (nbr_intf) {
      url += `&nbr_intf=${nbr_intf}`;
    }
    const {
      code,
      msg = i18nRender('businessNet.tips9'),
      data = [],
    } = await requestHttp({
      api: url,
      method: 'GET',
    });

    commit('SET_VLAN_DATA', data);
    return { code, msg };
  },
};

const getters = {};

export default {
  namespaced: true, //开启命名空间，更好的区分使用stage,防止不同stage模块中的方法重名
  state,
  getters,
  mutations,
  actions,
};
